package io.kiki.sba.registry.client.provider;

import io.kiki.sba.registry.client.api.Configurator;
import io.kiki.sba.registry.client.api.Publisher;
import io.kiki.sba.registry.client.api.Subscriber;
import io.kiki.sba.registry.client.util.StringUtils;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Setter
@Getter
public class RegisterCache {


    private Map<String, Publisher> registIdToPublisherMap = new ConcurrentHashMap<String, Publisher>();

    private Map<String, Subscriber> registIdToSubscriberMap = new ConcurrentHashMap<String, Subscriber>();

    private Map<String, Configurator> registIdToConfiguratorMap = new ConcurrentHashMap<String, Configurator>();


    public void addRegister(@NonNull Publisher publisher) {
        if (StringUtils.isEmpty(publisher.getDataId())) {
            return;
        }

        registIdToPublisherMap.put(publisher.getRegistId(), publisher);
    }


    public void addRegister(@NonNull Subscriber subscriber) {
        if (StringUtils.isEmpty(subscriber.getDataId())) {
            return;
        }

        registIdToSubscriberMap.put(subscriber.getRegistId(), subscriber);
    }


    public void addRegister(@NonNull Configurator configurator) {
        if (StringUtils.isEmpty(configurator.getDataId())) {
            return;
        }

        registIdToConfiguratorMap.put(configurator.getRegistId(), configurator);
    }


    public void remove(String registId) {
        registIdToPublisherMap.remove(registId);
        registIdToSubscriberMap.remove(registId);
        registIdToConfiguratorMap.remove(registId);
    }


    public List<Publisher> getPublisherList() {
        return new ArrayList<Publisher>(registIdToPublisherMap.values());
    }


    public List<Subscriber> getSubscriberList() {
        return new ArrayList<Subscriber>(registIdToSubscriberMap.values());
    }


    public List<Configurator> getConfiguratorList() {
        return new ArrayList<Configurator>(registIdToConfiguratorMap.values());
    }
}
